﻿using System;
using System.Collections.Generic;

namespace UnityEngine
{
	// Token: 0x020000F6 RID: 246
	public class IsoSphere
	{
		// Token: 0x06000571 RID: 1393 RVA: 0x000215A8 File Offset: 0x0001F7A8
		public static int getMiddlePoint(int p1, int p2, ref List<Vector3> vertices, ref Dictionary<long, int> cache, float radius)
		{
			bool flag = p1 < p2;
			long num = (long)((!flag) ? p2 : p1);
			long num2 = (long)((!flag) ? p1 : p2);
			long key = (num << 32) + num2;
			int result;
			if (cache.TryGetValue(key, out result))
			{
				return result;
			}
			Vector3 vector = vertices[p1];
			Vector3 vector2 = vertices[p2];
			Vector3 vector3 = new Vector3((vector.x + vector2.x) / 2f, (vector.y + vector2.y) / 2f, (vector.z + vector2.z) / 2f);
			int count = vertices.Count;
			vertices.Add(vector3.normalized * radius);
			cache.Add(key, count);
			return count;
		}

		// Token: 0x06000572 RID: 1394 RVA: 0x00021678 File Offset: 0x0001F878
		public static void CreateMesh(GameObject gameObject, int recursionLevel, float radius, bool visible)
		{
			MeshRenderer meshRenderer = gameObject.GetComponent<MeshRenderer>();
			if (meshRenderer == null)
			{
				meshRenderer = gameObject.AddComponent<MeshRenderer>();
			}
			meshRenderer.enabled = visible;
			MeshFilter meshFilter = gameObject.GetComponent<MeshFilter>();
			if (meshFilter == null)
			{
				meshFilter = gameObject.AddComponent<MeshFilter>();
			}
			Mesh mesh = meshFilter.mesh;
			mesh.Clear();
			List<Vector3> list = new List<Vector3>();
			Dictionary<long, int> dictionary = new Dictionary<long, int>();
			float num = (1f + Mathf.Sqrt(5f)) / 2f;
			List<Vector3> list2 = list;
			Vector3 vector = new Vector3(-1f, num, 0f);
			list2.Add(vector.normalized * radius);
			List<Vector3> list3 = list;
			Vector3 vector2 = new Vector3(1f, num, 0f);
			list3.Add(vector2.normalized * radius);
			List<Vector3> list4 = list;
			Vector3 vector3 = new Vector3(-1f, -num, 0f);
			list4.Add(vector3.normalized * radius);
			List<Vector3> list5 = list;
			Vector3 vector4 = new Vector3(1f, -num, 0f);
			list5.Add(vector4.normalized * radius);
			List<Vector3> list6 = list;
			Vector3 vector5 = new Vector3(0f, -1f, num);
			list6.Add(vector5.normalized * radius);
			List<Vector3> list7 = list;
			Vector3 vector6 = new Vector3(0f, 1f, num);
			list7.Add(vector6.normalized * radius);
			List<Vector3> list8 = list;
			Vector3 vector7 = new Vector3(0f, -1f, -num);
			list8.Add(vector7.normalized * radius);
			List<Vector3> list9 = list;
			Vector3 vector8 = new Vector3(0f, 1f, -num);
			list9.Add(vector8.normalized * radius);
			List<Vector3> list10 = list;
			Vector3 vector9 = new Vector3(num, 0f, -1f);
			list10.Add(vector9.normalized * radius);
			List<Vector3> list11 = list;
			Vector3 vector10 = new Vector3(num, 0f, 1f);
			list11.Add(vector10.normalized * radius);
			List<Vector3> list12 = list;
			Vector3 vector11 = new Vector3(-num, 0f, -1f);
			list12.Add(vector11.normalized * radius);
			List<Vector3> list13 = list;
			Vector3 vector12 = new Vector3(-num, 0f, 1f);
			list13.Add(vector12.normalized * radius);
			List<IsoSphere.TriangleIndices> list14 = new List<IsoSphere.TriangleIndices>();
			list14.Add(new IsoSphere.TriangleIndices(0, 11, 5));
			list14.Add(new IsoSphere.TriangleIndices(0, 5, 1));
			list14.Add(new IsoSphere.TriangleIndices(0, 1, 7));
			list14.Add(new IsoSphere.TriangleIndices(0, 7, 10));
			list14.Add(new IsoSphere.TriangleIndices(0, 10, 11));
			list14.Add(new IsoSphere.TriangleIndices(1, 5, 9));
			list14.Add(new IsoSphere.TriangleIndices(5, 11, 4));
			list14.Add(new IsoSphere.TriangleIndices(11, 10, 2));
			list14.Add(new IsoSphere.TriangleIndices(10, 7, 6));
			list14.Add(new IsoSphere.TriangleIndices(7, 1, 8));
			list14.Add(new IsoSphere.TriangleIndices(3, 9, 4));
			list14.Add(new IsoSphere.TriangleIndices(3, 4, 2));
			list14.Add(new IsoSphere.TriangleIndices(3, 2, 6));
			list14.Add(new IsoSphere.TriangleIndices(3, 6, 8));
			list14.Add(new IsoSphere.TriangleIndices(3, 8, 9));
			list14.Add(new IsoSphere.TriangleIndices(4, 9, 5));
			list14.Add(new IsoSphere.TriangleIndices(2, 4, 11));
			list14.Add(new IsoSphere.TriangleIndices(6, 2, 10));
			list14.Add(new IsoSphere.TriangleIndices(8, 6, 7));
			list14.Add(new IsoSphere.TriangleIndices(9, 8, 1));
			for (int i = 0; i < recursionLevel; i++)
			{
				List<IsoSphere.TriangleIndices> list15 = new List<IsoSphere.TriangleIndices>();
				foreach (IsoSphere.TriangleIndices triangleIndices in list14)
				{
					int middlePoint = IsoSphere.getMiddlePoint(triangleIndices.v1, triangleIndices.v2, ref list, ref dictionary, radius);
					int middlePoint2 = IsoSphere.getMiddlePoint(triangleIndices.v2, triangleIndices.v3, ref list, ref dictionary, radius);
					int middlePoint3 = IsoSphere.getMiddlePoint(triangleIndices.v3, triangleIndices.v1, ref list, ref dictionary, radius);
					list15.Add(new IsoSphere.TriangleIndices(triangleIndices.v1, middlePoint, middlePoint3));
					list15.Add(new IsoSphere.TriangleIndices(triangleIndices.v2, middlePoint2, middlePoint));
					list15.Add(new IsoSphere.TriangleIndices(triangleIndices.v3, middlePoint3, middlePoint2));
					list15.Add(new IsoSphere.TriangleIndices(middlePoint, middlePoint2, middlePoint3));
				}
				list14 = list15;
			}
			mesh.vertices = list.ToArray();
			List<int> list16 = new List<int>();
			for (int j = 0; j < list14.Count; j++)
			{
				list16.Add(list14[j].v1);
				list16.Add(list14[j].v2);
				list16.Add(list14[j].v3);
			}
			mesh.triangles = list16.ToArray();
			mesh.uv = new Vector2[mesh.vertices.Length];
			Vector3[] array = new Vector3[list.Count];
			for (int k = 0; k < array.Length; k++)
			{
				array[k] = list[k].normalized;
			}
			mesh.normals = array;
			mesh.RecalculateBounds();
			mesh.Optimize();
		}

		// Token: 0x020000F7 RID: 247
		public struct TriangleIndices
		{
			// Token: 0x06000573 RID: 1395 RVA: 0x00021C00 File Offset: 0x0001FE00
			public TriangleIndices(int v1, int v2, int v3)
			{
				this.v1 = v1;
				this.v2 = v2;
				this.v3 = v3;
			}

			// Token: 0x0400058B RID: 1419
			public int v1;

			// Token: 0x0400058C RID: 1420
			public int v2;

			// Token: 0x0400058D RID: 1421
			public int v3;
		}
	}
}
